package com.kfm.vue.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kfm.vue.model.RoleModel;
import com.kfm.vue.service.RoleService;
import com.kfm.vue.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @GetMapping
    public ModelAndView index(ModelAndView mv){
        mv.setViewName("role/index");
        return mv;
    }

    @GetMapping("/list")
    public Resp list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                     @RequestParam(value= "size", defaultValue = "2") Integer size){
        Page<RoleModel> modelPage = new Page<>(page, size);
        roleService.page(modelPage);
        return Resp.ok(modelPage);
    }

    @PostMapping("/add")
    public Resp add(@RequestBody RoleModel roleModel){
        boolean save;
        if (roleModel.getId() != null) {
            save = roleService.updateById(roleModel);
        } else {
            save = roleService.save(roleModel);
        }
        if (save) {
            return Resp.ok("操作成功");
        } else {
            return Resp.fail("操作失败");
        }
    }

    @DeleteMapping("/{id}")
    public Resp delete(@PathVariable("id") Integer id){
        boolean remove = roleService.removeById(id);
        if (remove) {
            return Resp.ok("删除成功");
        } else {
            return Resp.fail("删除失败");
        }
    }


}
